﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using VietPremier.Areas.AdminCP.Models;
using MvcLibrary.Repository;
using System.IO;

namespace VietPremier.Areas.AdminCP.Controllers
{
    public class AdminDestinationCountryController : Controller
    {
        //
        // GET: /AdminCP/AdminTravelCities/

        public ActionResult Index(int id)
        {
            using (dbVietPremierDataContext db = new dbVietPremierDataContext())
            {
                var query = from p in db.EntityDestinationCountries
                            join q in db.EntityDestinationAreas on p.IdDestinationArea equals q.Id
                            where p.IdDestinationArea == id
                            select new AdminDestinationCountryModel
                            {
                                Id = p.Id,
                                IdDestinationArea = p.IdDestinationArea,
                                DestinationAreaName = q.Name,
                                Name = p.Name,
                                Description = p.Description,
                                Body = p.Body,
                                Image = p.Image,
                                Position = p.Position

                            };
                ViewBag.DestinationAreaName = db.EntityDestinationAreas.FirstOrDefault(p=>p.Id==id).Name;
                ViewBag.IdDestinationArea = id;
                return View(query.ToList());
                
            }
        }

        public ActionResult Edit(int id)
        {
            using (dbVietPremierDataContext db = new dbVietPremierDataContext())
            {
                var query = from p in db.EntityDestinationCountries
                            where p.Id == id
                            select new AdminDestinationCountryModel
                            {
                                Id = p.Id,
                                IdDestinationArea = p.IdDestinationArea,
                                Name = p.Name,
                                Description = p.Description,
                                Body = p.Body,
                                Image = p.Image,
                                Position = p.Position,
                                Meta_keyword = p.Meta_keyword,
                                Meta_description = p.Meta_description,
                                Titletag = p.Titletag
                            };
                ViewBag.IdDestinationArea = query.FirstOrDefault().IdDestinationArea;
                return View(query.FirstOrDefault());
            }
        }

        [ValidateInput(false)]
        [HttpPost]
        public ActionResult Edit(AdminDestinationCountryModel model, HttpPostedFileBase Image)
        {
            using (dbVietPremierDataContext db = new dbVietPremierDataContext())
            {
                EntityDestinationCountry Country = db.EntityDestinationCountries.FirstOrDefault(p => p.Id == model.Id);
                if (Country != null)
                {
                    Country.IdDestinationArea = model.IdDestinationArea;
                    Country.Name = model.Name;
                    Country.Description = model.Description;
                    Country.Body = model.Body;
                    Country.Position = model.Position;
                    Country.Meta_description = model.Meta_description;
                    Country.Meta_keyword = model.Meta_keyword;
                    Country.Titletag = model.Titletag;
                    if (Image != null && Image.ContentLength > 0)
                    {
                        int kb = Image.ContentLength / 1024; //file size kb
                        if (kb >= 2048) // file qua lon
                        {
                            return RedirectToAction("Index", "Error", new { errorMsg = "Images must be less than 2MB." });
                        }
                        //delete file cu
                        if (Country.Image != null)
                        {
                            string fileToDelete = Path.Combine(Server.MapPath("~/Content/upload/destination/"), Country.Image); // file hinh cu
                            System.IO.File.Delete(fileToDelete);
                        }

                        ImageHelper imgHelper = new ImageHelper();
                        string encodestring = imgHelper.encodeImageFile(Image);

                        if (encodestring == "!")
                            return RedirectToAction("Index", "Error", new { errorMsg = "Can not upload image" });

                        var path = Path.Combine(Server.MapPath("~/Content/upload/destination/"), encodestring);
                        Image.SaveAs(path);

                        Country.Image = encodestring;
                    }
                    db.SubmitChanges();
                    return RedirectToAction("Index", new { id = model.IdDestinationArea });
                }
                    else
                    return View("Error");

                
            }
        }

        public ActionResult Insert(int id)
        {
            ViewBag.IdDestinationArea = id;
            return View();
        }

        [ValidateInput(false)]
        [HttpPost]
        public ActionResult Insert(AdminDestinationCountryModel model, int IdDestinationArea, HttpPostedFileBase Image)
        {
            using (dbVietPremierDataContext db = new dbVietPremierDataContext())
            {
                EntityDestinationCountry Country = new EntityDestinationCountry();
                Country.IdDestinationArea = IdDestinationArea;
                Country.Name = model.Name;
                Country.Description = model.Description;
                Country.Body = model.Body;
                Country.Position = model.Position;
                Country.Meta_description = model.Meta_description;
                Country.Meta_keyword = model.Meta_keyword;
                Country.Titletag = model.Titletag;
                if (Image != null && Image.ContentLength > 0)
                {
                    int kb = Image.ContentLength / 1024; //file size kb
                    if (kb >= 2048) // file qua lon
                    {
                        return RedirectToAction("Index", "Error", new { errorMsg = "Images must be less than 2MB." });
                    }
                    //delete file cu
                    if (Country.Image != null)
                    {
                        string fileToDelete = Path.Combine(Server.MapPath("~/Content/upload/destination/"), Country.Image); // file hinh cu
                        System.IO.File.Delete(fileToDelete);
                    }

                    ImageHelper imgHelper = new ImageHelper();
                    string encodestring = imgHelper.encodeImageFile(Image);

                    if (encodestring == "!")
                        return RedirectToAction("Index", "Error", new { errorMsg = "Can not upload image" });

                    var path = Path.Combine(Server.MapPath("~/Content/upload/destination/"), encodestring);
                    Image.SaveAs(path);

                    Country.Image = encodestring;
                }
                db.EntityDestinationCountries.InsertOnSubmit(Country);
                db.SubmitChanges();
                return RedirectToAction("Index", new { id = model.IdDestinationArea });
            }
        }

        public string Delete(int id)
        {
            using (dbVietPremierDataContext db = new dbVietPremierDataContext())
            {
                var delquery = db.EntityDestinations.Where(p => p.IdDestinationCountry == id).ToList();
                db.EntityDestinations.DeleteAllOnSubmit(delquery);
                EntityDestinationCountry Country = db.EntityDestinationCountries.FirstOrDefault(p => p.Id == id);
                if (Country != null)
                {
                    if (Country.Image != null)
                    {
                        string fileToDelete = Path.Combine(Server.MapPath("~/Content/upload/destination/"), Country.Image); // file hinh cu
                        System.IO.File.Delete(fileToDelete);
                    }
                    db.EntityDestinationCountries.DeleteOnSubmit(Country);
                    db.SubmitChanges();
                    return "Ok";
                }
                else
                    return "Error";
            }
        }

    }
}
